package com.eshop.utils;

import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import java.util.Enumeration;
import java.net.InetAddress;
import java.net.NetworkInterface;

public class IpUtil
{
    private IpUtil() {
    }
    
    public static String getLocalIP() {
        String sIP = "";
        InetAddress ip = null;
        try {
            boolean bFindIP = false;
            for (Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); netInterfaces.hasMoreElements() && !bFindIP; bFindIP = true) {
                final NetworkInterface ni = netInterfaces.nextElement();
                final Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = ips.nextElement();
                    if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
                        break;
                    }
                }
            }
        }
        catch (Exception ex) {}
        if (null != ip) {
            sIP = ip.getHostAddress();
        }
        return sIP;
    }
    
    public static List<String> getLocalIPS() {
        InetAddress ip = null;
        final List<String> ipList = new ArrayList<String>();
        try {
            final Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                final NetworkInterface ni = netInterfaces.nextElement();
                final Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = ips.nextElement();
                    if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
                        ipList.add(ip.getHostAddress());
                    }
                }
            }
        }
        catch (Exception ex) {}
        return ipList;
    }
    
    public static String getMacId() {
        String macId = "";
        InetAddress ip = null;
        NetworkInterface ni = null;
        try {
            boolean bFindIP = false;
            for (Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces(); netInterfaces.hasMoreElements() && !bFindIP; bFindIP = true) {
                ni = netInterfaces.nextElement();
                final Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = ips.nextElement();
                    if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
                        break;
                    }
                }
            }
        }
        catch (Exception ex) {}
        if (null != ip) {
            try {
                macId = getMacFromBytes(ni.getHardwareAddress());
            }
            catch (SocketException ex2) {}
        }
        return macId;
    }
    
    public static List<String> getMacIds() {
        InetAddress ip = null;
        NetworkInterface ni = null;
        final List<String> macList = new ArrayList<String>();
        try {
            final Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                ni = netInterfaces.nextElement();
                final Enumeration<InetAddress> ips = ni.getInetAddresses();
                while (ips.hasMoreElements()) {
                    ip = ips.nextElement();
                    if (!ip.isLoopbackAddress() && ip.getHostAddress().matches("(\\d{1,3}\\.){3}\\d{1,3}")) {
                        macList.add(getMacFromBytes(ni.getHardwareAddress()));
                    }
                }
            }
        }
        catch (Exception ex) {}
        return macList;
    }
    
    private static String getMacFromBytes(final byte[] bytes) {
        final StringBuffer mac = new StringBuffer();
        boolean first = false;
        for (final byte b : bytes) {
            if (first) {
                mac.append("-");
            }
            byte currentByte = (byte)((b & 0xF0) >> 4);
            mac.append(Integer.toHexString(currentByte));
            currentByte = (byte)(b & 0xF);
            mac.append(Integer.toHexString(currentByte));
            first = true;
        }
        return mac.toString().toUpperCase();
    }
}
